package com.hmall.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.clients.AddressClient;
import com.hmall.clients.ItemClient;
import com.hmall.common.dto.Item;
import com.hmall.order.interceptor.UserInterceptor;
import com.hmall.order.mapper.OrderDetailMapper;
import com.hmall.order.mapper.OrderLogisticsMapper;
import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.pojo.OrderLogistics;
import com.hmall.order.pojo.OrderMassage;
import com.hmall.order.service.IOrderService;
import com.hmall.pojo.Address;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yaml.snakeyaml.events.Event;

import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;

@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> implements IOrderService {
    /**
     * 生成订单
     *
     * @param orderMassage
     * @return
     */
    @Autowired
    private ItemClient itemClient;

    @Autowired
    private AddressClient addressClient;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private OrderLogisticsMapper orderLogisticsMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createOrder(OrderMassage orderMassage) {
        //1.查询对应id的商品信息
        Item item = itemClient.save(orderMassage.getItemId());
        //2.获取用户信息
        Long userId = UserInterceptor.getUserId();
        //3.将订单的数据填充
        Order order = new Order();
        order.setTotalFee(orderMassage.getNum() * item.getPrice());//总金额
        order.setPaymentType(orderMassage.getPaymentType());//支付类型
        order.setUserId(userId);
        order.setCreateTime(Timestamp.valueOf(LocalDateTime.now()));
        order.setStatus(1);//设置支付状态
        int insert = orderMapper.insert(order);
        if (insert > 0) {
            //4.将订单插入订单表，并返回获取返回的id
            Long id = order.getId();
            //5.将订单详细信息插入订单详情表
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(id);
            orderDetail.setNum(orderMassage.getNum());
            orderDetail.setImage(item.getImage());
            orderDetail.setPrice(item.getPrice());
            orderDetail.setSpec(item.getSpec());
            orderDetail.setItemId(item.getId());
            orderDetail.setName(item.getName());
            orderDetailMapper.insert(orderDetail);
            //6.封装tb_order_detail表
            OrderLogistics orderLogistics = new OrderLogistics();
            Address address = addressClient.findAddressById(orderMassage.getAddressId());
            orderLogistics.setOrderId(id);//设置订单id
            orderLogistics.setCity(address.getContact());//设置用户名
            orderLogistics.setMobile(address.getMobile());//设置电话号
            orderLogistics.setProvince(address.getProvince());
            orderLogistics.setCity(address.getCity());
            orderLogistics.setTown(address.getTown());
            orderLogistics.setStreet(address.getStreet());
            orderLogistics.setContact(address.getContact());
            orderLogistics.setUpdateTime(Timestamp.valueOf(LocalDateTime.now()));
            orderLogisticsMapper.insert(orderLogistics);

            //7.扣减库存
            itemClient.updateStock(item.getId(),orderMassage.getNum());
            //返回订单id
            return id;
        } else {
            throw new RuntimeException("订单创建失败");
        }

    }
}
